package com.igeek.collateral.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.Assess;
import com.igeek.collateral.entity.Collateral;
import com.igeek.collateral.mapper.AssessMapper;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.ICollateralService;
import com.igeek.collateral.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * TODO
 *
 * @author chh
 * @since 2024/7/4
 **/
@RestController
@RequestMapping("/admin/collateral")
@Api(tags = "押品信息接口")
@Slf4j
public class CollateralController {
    @Autowired
    private ICollateralService iCollateralService;



    //通过id进行查询
    @GetMapping("/{id}")
    @ApiOperation("根据id查询押品信息")
    public Result findOne(@PathVariable("id") Long id)
    {
        try {
            System.out.println("id为:"+id);
            Collateral co = iCollateralService.getById(id);
            log.info(String.valueOf(co));
            if(co!=null) {
                return new Result(true, "查询成功", co);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"查询失败");
    }



    //分页查询
    @GetMapping("findPage")
    @ApiOperation("条件分页查询押品信息")
    public Result findPage(PageQueryVO vo){
        try {
            Page<Collateral> page = iCollateralService.findPage(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }


    //分页查询(只查询待审核的)
    @GetMapping("findPageAss")
    @ApiOperation("条件分页查询押品信息")
    public Result findPageAssess(PageQueryVO vo){
        try {
            Page<Collateral> page = iCollateralService.findPageAss(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }


    //
    //分页查询(只查询待审核的)
    @GetMapping("findPageQ")
    @ApiOperation("条件分页查询押品信息")
    public Result findPageQ(PageQueryVO vo){
        try {
            Page<Collateral> page = iCollateralService.findPageQ(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }



    //查询所有符合条件的押品(Pie状态)
    @GetMapping("findPie")
    @ApiOperation("查询的押品饼状图信息")
    public Result findAllPie()
    {
        try {
            List<PieVO> pieMess = iCollateralService.getPieMess();
            if (pieMess != null) {
                return new Result(true, "查询成功", pieMess);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }



    //获取柱状图的信息
    @GetMapping("findColumn")
    @ApiOperation("查询的押品柱状图信息")
    public Result findAllColumn()
    {
        try {
            ColumnVo colMess = iCollateralService.getColumnMess();
            if (colMess != null) {
                return new Result(true, "查询成功", colMess);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }


    //查询所有符合的押品(折线状态)
    @GetMapping("findZhe")
    @ApiOperation("查询的押品折线图信息")
    public Result findAllZhe()
    {
        try {
            ZheVo zheMess = iCollateralService.getZheMess();
            if (zheMess != null) {
                return new Result(true, "查询成功", zheMess);
            }
        }catch(Exception e)
            {
                e.printStackTrace();
            }
        return new Result(false,"查询失败");
    }




    //查询双折现图的信息呈现





    //删除操作，根据用户id删除（单表的）
//    @DeleteMapping("{id}")
//    @ApiOperation("根据id删除押品信息")
//    public Result delete (@PathVariable("id") Long id)
//    {
//        try {
//            boolean flag=iCollateralService.delete(id);
//            if(flag)
//            {
//                return new Result(true,"删除成功");
//            }
//        } catch (RuntimeException e) {
//            return new Result(false,e.getMessage());
//        } catch (Exception e)
//        {
//            e.printStackTrace();
//        }
//        return new Result(false,"删除失败");
//    }



    //一对多的删除（一对多的进库信息删除）
    @DeleteMapping("{id}")
    @ApiOperation("删除押品信息")
    public Result deleteAll (@PathVariable("id") Integer id)
    {
        try {
            boolean flag=iCollateralService.deleteAll(id);
            if(flag)
            {
                return new Result(true,"删除成功");
            }
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new Result(false,"删除失败");
    }


    //修改押品信息(编辑)
    @PutMapping("update")
    @ApiOperation("编辑押品信息")
    @Transactional
    public Result update(@RequestBody Collateral co){
        try {
            boolean flag = iCollateralService.updateById(co);
            if(flag){
                return new Result(true , "更新成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "更新失败");
    }



    //修改对应的押品信息（点击同意后）
    @PutMapping("updateS")
    @ApiOperation("编辑押品信息")
    @Transactional
    public Result updateS(@RequestBody Collateral co){
        try {
            boolean flag = iCollateralService.updateS(co);
            if(flag){
                return new Result(true , "更新成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "更新失败");
    }


    //修改信息，点击驳回后
    @PutMapping("updateDS")
    @ApiOperation("编辑押品信息")
    @Transactional
    public Result updateDS(@RequestBody Collateral co){
        try {
            boolean flag = iCollateralService.updateDS(co);
            if(flag){
                return new Result(true , "更新成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "更新失败");
    }





    //根据新增的
    @PostMapping("add")
    @ApiOperation("添加押品信息")
    public Result add(@RequestBody  Collateral co)
    {
        try {
            boolean save = iCollateralService.save(co);
            if(save)
            {
                //确保更新好后，拿到该新增的押品id
                Integer id = co.getId();
                return new Result(true,"添加成功",id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"添加失败");
    }



    @PostMapping("upload")
    @ApiOperation("押品图片上传")
    public Result upload(MultipartFile imgFile) throws IOException {
        //返回的是图片名字，拼接上七牛云的域名进行显示
        String pic = iCollateralService.uploadByQiniuyun(imgFile);

        //返回的是外链地址，可用于直接显示
        //String pic = setmealService.uploadByAliyun(imgFile);
        if(StringUtils.hasLength(pic)){
            return new Result(true,"上传图片成功", (Object) pic);
        }
        return new Result(false , "上传图片失败");
    }



    //拿到历史流程相关的信息
    @GetMapping("historyMess")
    @ApiOperation("历史流程信息")
    public Result historyMess(@PathVariable("id") Long id)
    {
        try {
            HistoryMessVo history= iCollateralService.getHMess(id);
            if(!ObjectUtils.isEmpty(history))
            {
               return new Result(true,"查询成功",history);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"查询失败");
    }



    //拿到当前的进库数量以及出库数量
    @GetMapping("topMessNum")
    @ApiOperation("当前的进库与出库数量")
    public Result topMessNum()
    {
        try {
            InOutAllNumVo inOutAllNumVo = iCollateralService.getTopMess();
            if(!ObjectUtils.isEmpty(inOutAllNumVo))
            {
                return new Result(true,"统计成功",inOutAllNumVo);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
       return new Result(false,"统计失败");
    }




    //修改押品状态
//    @PutMapping("updateCollS")
//    @ApiOperation("修改押品状态")
//    public Result updateCollS()



}
